package cn.jolyoulu.common.mybatis.plus.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Map;

/**
 * @Author: JolyouLu
 * @Date: 2023/3/19 17:17
 * @Description
 */
public class QueryWrapperUtils {

    /**
     * 返回sql，带when
     * @param ew
     * @return
     */
    public static String getWhenSql(LambdaQueryWrapper<?> ew){
        String sql = ew.getCustomSqlSegment();
        Map<String, Object> paramNameValuePairs = ew.getParamNameValuePairs();
        for (Map.Entry<String, Object> entry : paramNameValuePairs.entrySet()) {
            sql = sql.replaceAll("#\\{ew.paramNameValuePairs\\."+entry.getKey()+"}", "'"+entry.getValue()+"'");
        }
        return sql;
    }

}
